//
//  MeInterestCell.m
//  ZITHER
//
//  Created by weixin on 2018/10/16.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MeInterestCell.h"

@implementation MeInterestCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUI];
    [self setMasonry];
    
    BaseView * view1 = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
    view1.backgroundColor = [UIColor colorWithHex:@"eeeeee"];
    [self addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-1);
        make.left.right.equalTo(self).offset(1);
        make.height.mas_equalTo(1);
    }];
}

-(void)setModelSub:(MyselfFollowListModelSub *)modelsub{
    _modelSub = modelsub;
    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,modelsub.avatar];
    [self.PicUser sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    self.labUserName.text = modelsub.nickName;
    
    if ( [modelsub.followMeStatus isEqualToString:@"0"]){
        self.labContent.text = @"";
        [self.labUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.PicUser.mas_centerY).offset(0);
            make.left.equalTo(self.PicUser.mas_right).offset(8);
        }];
    }else{
        self.labContent.text = @"已关注我";
        [self.labUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.PicUser).offset(0);
            make.left.equalTo(self.PicUser.mas_right).offset(8);
        }];
    }

    self.InterestBtn.selected = ![modelsub.followStatus isEqualToString:@"0"]?NO:YES;
    
    if (self.InterestBtn.selected) {
        //加关注
        self.InterestBtn.backgroundColor = [UIColor whiteColor];
    }else{
        //已关注
        self.InterestBtn.backgroundColor = baseColorTheme;
    }
}


-(void)setUI{
    [self addSubview:self.PicUser];
    [self addSubview:self.labUserName];
    [self addSubview:self.labContent];
    [self addSubview:self.InterestBtn];
}

-(void)setMasonry{
    
    [self.PicUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(20);
        make.left.equalTo(self).offset(14);
        make.width.height.mas_equalTo(50);
        make.bottom.equalTo(self).offset(-20);
    }];
    
    [self.labUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PicUser).offset(0);
        make.left.equalTo(self.PicUser.mas_right).offset(8);
    }];
    
    [self.labContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labUserName.mas_bottom).offset(8);
        make.left.equalTo(self.PicUser.mas_right).offset(8);
    }];
    
    [self.InterestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.PicUser.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-14);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(32);
    }];
}

-(BaseImageV *)PicUser
{
    if (!_PicUser) {
        _PicUser = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        [_PicUser setCornerRadiusWithRadius:25 isMasks:YES];
    }
    return _PicUser;
}
-(BaseLab *)labUserName
{
    if (!_labUserName) {
        _labUserName = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"用户名称" font:18 textColor:nil isBold:NO numberOfLines:1];
        _labUserName.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];

    }
    return _labUserName;
}
-(BaseLab *)labContent
{
    if (!_labContent) {
        _labContent = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"是否关注" font:14 textColor:[UIColor colorWithHex:@"999999"] isBold:NO numberOfLines:1];
        _labContent.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];

    }
    return _labContent;
}

-(BaseBtn *)InterestBtn
{
    if (!_InterestBtn) {
        _InterestBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"已关注" NormalTextColor:[UIColor whiteColor] SeleText:@"关注" SeleTextColor:baseColorTheme  font:12 isBold:NO];
        _InterestBtn.backgroundColor = baseColorTheme;
        [_InterestBtn setCornerRadiusWithRadius:4 isMasks:YES];
        [_InterestBtn setBorderWithColor:baseColorTheme width:1];
        
        
//        [_InterestBtn setImage:[UIImage imageNamed:@"mine_btn_more"] forState:UIControlStateSelected];
        @weakify(self);
        [[_InterestBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            x.selected = !x.selected;
            if (x.selected) {
                //加关注
                self.InterestBtn.backgroundColor = [UIColor whiteColor];
            }else{
                //已关注
                self.InterestBtn.backgroundColor = baseColorTheme;
            }
            
            
            if (!x.selected) {
                self.InterestBtn.backgroundColor = [UIColor whiteColor];
                NSString *str = nil;
                if (self.type == 0) {
                    str = self.modelSub.followUserId;
                }else{
                    str = self.modelSub.userId;
                }
                [[serviceDefualt loadAddUserFollowFollowUserId:str reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"关注成功"];
                        self.InterestBtn.backgroundColor = baseColorTheme;
                        
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                             [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.InterestBtn.selected = ! self.InterestBtn.selected;
                        if (self.InterestBtn.selected) {
                            //加关注
                            self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        }else{
                            //已关注
                            self.InterestBtn.backgroundColor = baseColorTheme;
                        }
                    }
                }];
                
                
            }else{
                self.InterestBtn.backgroundColor = baseColorTheme;
                NSString *str = nil;
                if (self.type == 0) {
                    str = self.modelSub.followUserId;
                }else{
                    str = self.modelSub.userId;
                }
            
                
                [[serviceDefualt loadCancelUserFollowFollowUserId:str reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"取消关注成功"];
                        self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                             [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.InterestBtn.selected = ! self.InterestBtn.selected;
                        if (self.InterestBtn.selected) {
                            //加关注
                            self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        }else{
                            //已关注
                            self.InterestBtn.backgroundColor = baseColorTheme;
                        }
                    }
                }];
                
            }
        }];
    }
    return _InterestBtn;
}
@end
